<template>
  <div v-if="!item.meta || !item.meta.hidden">
    <!-- 没有子菜单的路由 -->
    <template v-if="!hasOneShowingChild(item.children, item)">
      <el-menu-item 
        v-if="item.children && item.children.length === 0 && hasPermission(item)"
        :index="resolvePath(item.path)"
      >
        <el-icon v-if="item.meta && item.meta.icon">
          <component :is="item.meta.icon" />
        </el-icon>
        <template #title>
          <span>{{ item.meta ? item.meta.title : item.name }}</span>
        </template>
      </el-menu-item>
      
      <el-sub-menu 
        v-else-if="hasPermission(item)"
        :index="resolvePath(item.path)"
        popper-append-to-body
      >
        <template #title>
          <el-icon v-if="item.meta && item.meta.icon">
            <component :is="item.meta.icon" />
          </el-icon>
          <span>{{ item.meta ? item.meta.title : item.name }}</span>
        </template>
        
        <sidebar-item
          v-for="child in item.children"
          :key="child.path"
          :item="child"
          :base-path="resolvePath(child.path)"
          :is-collapse="isCollapse"
        />
      </el-sub-menu>
    </template>
    
    <!-- 只有一个子菜单的路由 -->
    <template v-else>
      <el-menu-item
        v-if="onlyOneChild.meta && !onlyOneChild.meta.hidden && hasPermission(onlyOneChild)"
        :index="resolvePath(onlyOneChild.path)"
      >
        <el-icon v-if="onlyOneChild.meta && onlyOneChild.meta.icon">
          <component :is="onlyOneChild.meta.icon" />
        </el-icon>
        <template #title>
          <span>{{ onlyOneChild.meta.title }}</span>
        </template>
      </el-menu-item>
    </template>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import path from 'path-browserify'
import { useUserStore } from '@/store/user'

const userStore = useUserStore()

const props = defineProps({
  item: {
    type: Object,
    required: true
  },
  basePath: {
    type: String,
    default: ''
  },
  isCollapse: {
    type: Boolean,
    default: false
  }
})

const onlyOneChild = ref(null)

// 检查是否有权限
const hasPermission = (route) => {
  if (!route.meta || !route.meta.permission) {
    return true
  }
  
  return userStore.hasPermission(route.meta.permission)
}

// 处理只有一个显示的子菜单
const hasOneShowingChild = (children = [], parent) => {
  if (!children) {
    children = []
  }
  
  const showingChildren = children.filter(item => {
    if (item.meta && item.meta.hidden) {
      return false
    } else {
      // 检查权限
      if (!hasPermission(item)) {
        return false
      }
      
      // Temp set(will be used if only has one showing child)
      onlyOneChild.value = item
      return true
    }
  })
  
  // 当只有一个子路由时，默认显示该子路由
  if (showingChildren.length === 1) {
    return true
  }
  
  // 没有子路由则显示父路由
  if (showingChildren.length === 0) {
    onlyOneChild.value = { ...parent, path: '', noShowingChildren: true }
    return true
  }
  
  return false
}

// 解析路由路径
const resolvePath = (routePath) => {
  if (routePath.startsWith('/')) {
    return routePath
  }
  return path.resolve(props.basePath, routePath)
}
</script>

<style scoped>
.el-menu-item, .el-sub-menu__title {
  height: 50px;
  line-height: 50px;
  transition: all 0.3s;
}

.el-menu-item:hover, .el-sub-menu__title:hover {
  background-color: rgba(255, 255, 255, 0.08) !important;
}

.el-menu-item.is-active {
  background-color: #1e88e5 !important; 
  color: #ffffff !important;
}

.el-menu-item i, .el-sub-menu__title i {
  color: inherit;
  margin-right: 10px;
  font-size: 18px;
  vertical-align: middle;
}

.el-sub-menu .el-menu-item {
  min-width: auto;
  padding-left: 50px !important;
}

.el-sub-menu.is-opened {
  background-color: rgba(0, 0, 0, 0.15);
}

/* 调整菜单项内部图标和文字间距 */
.item-wrapper {
  display: flex;
  align-items: center;
}

.item-title {
  margin-left: 8px;
  transition: opacity 0.3s;
}

/* 折叠状态下的样式调整 */
.is-collapse .item-title {
  display: none;
}

.is-collapse .el-menu-item, .is-collapse .el-sub-menu__title {
  padding: 0 16px !important;
  text-align: center;
}

.is-collapse .el-sub-menu .el-menu-item {
  padding-left: 16px !important;
}

.is-collapse .el-sub-menu .el-menu-item .item-title {
  display: inline-block;
}

/* 图标样式 */
.sidebar-icon {
  font-size: 18px;
  margin-right: 6px;
}
</style> 